package com.kvest.scanrecognizer.ui.fragment;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.kvest.scanrecognizer.R;
import com.kvest.scanrecognizer.adapter.ConvertingListAdapter;
import com.kvest.scanrecognizer.contentprovider.ScanRecognizerProviderMetadata;
import com.kvest.scanrecognizer.datastorage.table.RecognizeItemTable;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 16.09.13
 * Time: 20:35
 * To change this template use File | Settings | File Templates.
 */
public class ManagerFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>,
                                                                ConvertingListAdapter.OnActionListener{
    private static final int LOAD_RECOGNIZE_ITEMS_ID = 0;

    private ConvertingListAdapter adapter;

    private OnActionListener onActionListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.manager_fragment, container, false);

        String[] from = new String[]{RecognizeItemTable.NAME_COLUMN, RecognizeItemTable.THUMBNAIL_COLUMN, RecognizeItemTable.UID_COLUMN,
                                     RecognizeItemTable._ID, RecognizeItemTable.FAVORITE_COLUMN, RecognizeItemTable._ID,
                                     RecognizeItemTable.STATUS_COLUMN, RecognizeItemTable.STATUS_COLUMN, RecognizeItemTable.STATUS_COLUMN, RecognizeItemTable.STATUS_COLUMN,
                                     RecognizeItemTable.UID_COLUMN, RecognizeItemTable._ID, RecognizeItemTable.UID_COLUMN};
        int[] to = new int[]{R.id.name, R.id.thumbnail, R.id.download, R.id.delete, R.id.favorite, R.id.favorite,
                             R.id.download, R.id.share, R.id.mail, R.id.status, R.id.mail, R.id.name, R.id.share};
        adapter = new ConvertingListAdapter(getActivity(), R.layout.converting_list_item, null,
                                            from, to,ConvertingListAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        adapter.setOnActionListener(this);

        GridView gridView = (GridView)root.findViewById(R.id.manager_items_grid_view);
        gridView.setAdapter(adapter);

        return root;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            onActionListener = (OnActionListener)activity;
        } catch (ClassCastException cce) {
            onActionListener = null;
        }
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getActivity().getSupportLoaderManager().initLoader(LOAD_RECOGNIZE_ITEMS_ID, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
        switch (id) {
            case LOAD_RECOGNIZE_ITEMS_ID : return new CursorLoader(getActivity(), ScanRecognizerProviderMetadata.RECOGNIZE_ITEMS_URI,
                                                                   RecognizeItemTable.FULL_PROJECTION,null, null, null);
        }

        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        adapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        adapter.swapCursor(null);
    }

    @Override
    public void onEditName(long id) {
        if (onActionListener != null) {
            onActionListener.onEditName(id);
        }
    }

    @Override
    public void onDownload(String uid, int format) {
        if (onActionListener != null) {
            onActionListener.onDownload(uid, format);
        }
    }

    @Override
    public void onFavoriteChanged(long id, boolean value) {
        ContentValues values = new ContentValues(1);
        values.put(RecognizeItemTable.FAVORITE_COLUMN, (value ? 1 : 0));
        getActivity().getContentResolver().update(Uri.withAppendedPath(ScanRecognizerProviderMetadata.RECOGNIZE_ITEMS_URI,
                                                                       Long.toString(id)), values, null, null);
    }

    @Override
    public void onSendEmail(String uid, int format) {
        if (onActionListener != null) {
            onActionListener.onSendEmail(uid, format);
        }
    }

    @Override
    public void onShare(String uid, int format) {
        if (onActionListener != null) {
            onActionListener.onShare(uid, format);
        }
    }

    @Override
    public void onDelete(long id) {
        if (onActionListener != null) {
            onActionListener.onDelete(id);
        }
    }

    public interface OnActionListener {
        public void onEditName(long id);
        public void onDownload(String uid, int format);
        public void onSendEmail(String uid, int format);
        public void onShare(String uid, int format);
        public void onDelete(long id);
    }
}
